<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Node_model extends CI_Model{
    
    public function __construct() {
        parent::__construct();
//        $this->load->library('binarynode');
        $this->load->library('BinaryNode');
    }
    /**
     * Return root node object
     * @return BinaryNode
     */
    public function get_root(){
        return $this->get_node('0');
    }
    
    /**
     * 
     * @param int $id
     * @param boolean $sort
     * @param boolean $only_leaf
     * @return BinaryNode array
     */
    public function get_children($id,$sort=true,$only_leaf=false){
        $ary_children = array();
        $order = $sort?"ORDER BY IS_FOLDER DESC, R_PATH":"";
        $leaf = $only_leaf?"AND IS_FOLDER = 0 ":"";
        $query = $this->db->query("SELECT * FROM ".$this->config->item('resolv_table')." WHERE parent_id = ? ".$leaf.$order,array($id));
        foreach ($query->result_array() as $row){
            $node = new BinaryNode();
            $ary_children[$row['R_HASH']] = $node->set_by_rowdb($row);
        }
        return $ary_children;
    }
    
    /**
     * return node object
     * @param string $hash
     * @return BinaryNode
     */
    public function get_node($hash){
        $query = $this->db->query("SELECT * FROM ".$this->config->item('resolv_table')." WHERE r_hash = ? ",array($hash));
        $row = $query->row_array();
        
        return new BinaryNode($row);
//        $node = new BinaryNode();
//        return $node->set_by_rowdb($row);
    }
    
    /**
     * return node object
     * @param int $nade_id
     * @return BinaryNode
     */
    public function get_node_by_id($node_id){
        $query = $this->db->query("SELECT * FROM ".$this->config->item('resolv_table')." WHERE r_id = ? ",array($node_id));
        $row = $query->row_array();
        
        $node = new BinaryNode();
        return $node->set_by_rowdb($row);
    }
    
    /**
     * deferent element inside ?
     * @param string $hash_node
     * @return boolean
     */
    public function is_embed_elements($hash_node){
        $query = $this->db->query("select count(distinct(is_folder)) as rel from ".$this->config->item('resolv_table')." where parent_id = (select r_id from ".$this->config->item('resolv_table')." where r_hash = ?)",array($hash_node));
        $row = $query->row_array();
        return ((int)$row['REL'] > 1);
    }
    
    /**
     * return node object first in path
     * @param BinaryNode $node_obj
     * @return BinaryNode
     */
    public function get_first_node($node_obj){
        if($node_obj->get_parent_id() == 0){
            return $node_obj;
        }else{
            $str = str_replace($this->config->item('path_dir_rrd'),"",$node_obj->get_path());
            preg_match ("/_([0-9a-fA-F]{32})/", $str, $matches);
            return $this->get_node($matches[1]);
        }
    }
}